package com.itzyf.pokemondata.activity.panel

import android.app.Activity
import android.content.Intent
import android.text.TextUtils
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import com.avos.avoscloud.AVObject
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.itzyf.pokemondata.R
import com.itzyf.pokemondata.bean.DbPokemon
import com.itzyf.pokemondata.utils.OnRefreshListener
import com.itzyf.pokemondata.utils.setOnRefreshListener
import com.tydic.baselibrary.activity.BaseActivity
import kotlinx.android.synthetic.main.activity_list_star.*
import org.litepal.LitePal

/**
 * @author 依风听雨
 * @version 创建时间：2018/12/26 08:56
 */
class SelectPokemonActivity : BaseActivity() {
    var mDatas: MutableList<AVObject?> = ArrayList()
    var index = 0
    private var isStar = false
    private lateinit var mAdapter: QuickAdapter
    override fun getContentViewId() = R.layout.activity_list_star


    override fun handlerActivity() {
        mAdapter = QuickAdapter(R.layout.item_text, mDatas)
        rv_list.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this)
        rv_list.adapter = mAdapter
//        showProgressDialog()
        queryDatas()

        mAdapter.setOnLoadMoreListener({ queryDatas(index++) }, rv_list)

        pull_to_refresh.setOnRefreshListener(object : OnRefreshListener {
            override fun onRefresh() {
                index = 0
                queryDatas(index)
            }
        })

        mBtnSearch.setOnClickListener {
            val query = mSearchView.text.toString()
            queryDatas(query = query)
        }
        mSearchView.setOnEditorActionListener(TextView.OnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                val query = mSearchView.text.toString()
                queryDatas(query = query)
                return@OnEditorActionListener true
            }
            false
        })

        mAdapter.setOnItemClickListener { _, _, position ->
            val intent = Intent(this@SelectPokemonActivity, PanelDetailActivity::class.java)
            intent.putExtra("objId", mDatas[position]?.objectId)
            setResult(Activity.RESULT_OK, intent)
            finish()
        }

        ivStar.setOnClickListener {
            isStar = !isStar
            ivStar.setImageResource(if (isStar) R.drawable.ic_star_black_24dp else R.drawable.ic_star_border_black_24dp)
            index = 0
            queryDatas(index)
        }

    }


    /**
     * 查询数据
     */
    fun queryDatas(index: Int = 0, pageSize: Int = 20, query: String = "") {
        var where = ""
        if (!TextUtils.isEmpty(query) && isStar) {
            where = "pokemonName like '%$query%' and isStar = 1"
        } else {
            if (!TextUtils.isEmpty(query)) {
                where = "pokemonName like '%$query%'"
            }
            //属性筛选
            if (isStar) {
                where = "isStar = 1"
            }
        }

        val dataList =
            LitePal
                .select("pokemonName", "objectId", "property")
                .limit(pageSize)
                .offset(index * pageSize)
                .order("createdAt desc")
                .where(where)
                .find(DbPokemon::class.java)
        if (index == 0) {
            mDatas.clear()
            pull_to_refresh.finishRefresh()
        }

        for (obj in dataList) {
            val o = AVObject()
            o.put("pokemonName", obj.pokemonName)
            o.put("property", obj.property)
            o.objectId = obj.objectId
            mDatas.add(o)
        }

        if (dataList.size < pageSize) {
            mAdapter.loadMoreEnd()
        } else {
            mAdapter.loadMoreComplete()
        }
        mAdapter.notifyDataSetChanged()

    }


    internal inner class QuickAdapter(layoutResId: Int, data: List<AVObject?>) :
        BaseQuickAdapter<AVObject, BaseViewHolder>(layoutResId, data) {

        override fun convert(helper: BaseViewHolder, item: AVObject?) {
            item?.let {
                var property = it.getString("property")
                if (property.length == 18) {
                    property = "全属性"
                }
                helper.setText(R.id.text, "${it.getString("pokemonName")}($property)")
            }
        }
    }

    override fun getToolbarTitle() = "请选择精灵"

}